package com.xhldtc.login;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

public class RequestUtil
{
	public static Result sendGet(String url, Map<String, String> headers,
			Map<String, String> params, boolean useProxy)
	{
		DefaultHttpClient client = new DefaultHttpClient();
		if (useProxy)
		{
			client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
					getProxy());
		}

		HttpGet hp = new HttpGet(url + assembleParameters(params));

		if (headers != null)
		{
			hp.setHeaders(assembleHeaders(headers));
		}

		HttpResponse response = null;
		try
		{
			response = client.execute(hp);
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		HttpEntity entity = response.getEntity();

		Result result = new Result();
		result.setCookie(assembleCookies(client.getCookieStore().getCookies()));
		result.setStatusCode(response.getStatusLine().getStatusCode());
		result.setHeaders(response.getAllHeaders());
		result.setHttpEntity(entity);
		return result;
	}

	public static Result sendGet(String url, Map<String, String> headers,
			Map<String, String> params)
	{
		return sendGet(url, headers, params, false);
	}

	public static Result sendPost(String url, Map<String, String> headers,
			Map<String, String> params, String encoding, boolean useProxy)
	{
		DefaultHttpClient client = new DefaultHttpClient();
		if (useProxy)
		{
			client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
					getProxy());
		}

		HttpPost post = new HttpPost(url);

		List<NameValuePair> list = new ArrayList<NameValuePair>();
		for (String temp : params.keySet())
		{
			list.add(new BasicNameValuePair(temp, params.get(temp)));
		}

		try
		{
			post.setEntity(new UrlEncodedFormEntity(list, encoding));
		}
		catch (UnsupportedEncodingException e1)
		{
			e1.printStackTrace();
		}

		if (headers != null)
		{
			post.setHeaders(assembleHeaders(headers));
		}

		HttpResponse response = null;
		try
		{
			response = client.execute(post);
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		HttpEntity entity = response.getEntity();

		Result result = new Result();
		result.setStatusCode(response.getStatusLine().getStatusCode());
		result.setHeaders(response.getAllHeaders());
		result.setCookie(assembleCookies(client.getCookieStore().getCookies()));
		result.setHttpEntity(entity);
		return result;
	}

	public static Result sendPost(String url, Map<String, String> headers,
			Map<String, String> params, String encoding)
	{
		return sendPost(url, headers, params, encoding, false);
	}

	public static String assembleParameters(Map<String, String> params)
	{
		if (params == null || params.size() == 0)
		{
			return "";
		}

		StringBuilder sb = new StringBuilder("?");
		for (String key : params.keySet())
		{
			sb.append(key).append("=").append(params.get(key)).append("&");
		}
		return sb.substring(0, sb.length() - 1);
	}

	public static Header[] assembleHeaders(Map<String, String> map)
	{
		Header[] headers = new BasicHeader[map.size()];
		int i = 0;
		for (String key : map.keySet())
		{
			headers[i] = new BasicHeader(key, map.get(key));
			i++;
		}
		return headers;
	}

	public static String assembleCookies(List<Cookie> cookies)
	{
		if (cookies == null || cookies.size() == 0)
		{
			return "";
		}

		StringBuilder sb = new StringBuilder();
		for (Cookie c : cookies)
		{
			sb.append("; ").append(c.getName()).append("=")
					.append(c.getValue());
		}
		return sb.substring(2);
	}

	public static HttpHost getProxy()
	{
		return new HttpHost("10.240.45.225", 80);
	}

}
